﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Tomato.Authentication
{
    public class AjaxRequestAttribute : ActionFilterAttribute
    {
        public ViewDataDictionary ViewData { get; set; }

        public string ViewName { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.HttpContext.Request.IsAjaxRequest())
            {
                if (string.IsNullOrEmpty(ViewName))
                {
                    filterContext.Result = new ContentResult() { Content = "此方法不允许直接访问。" };
                }
                else
                {
                    filterContext.Result = new ViewResult() { ViewName = ViewName, ViewData = ViewData };
                }
            }

            base.OnActionExecuting(filterContext);
        }
    }
}
